package com.glory.erp.listener;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.glory.erp.auth.resources.bean.Resources;
import com.glory.erp.auth.resources.service.ResourcesService;

/**
 * @Description: 全资源加载监听器
 * @Title: AllResourceLoadListener.java
 * @Author: 樊双峰
 * @Date: 2018年5月23日 下午11:08:06
 * @Version V1.0
 */
public class AllResourceLoadListener implements ServletContextListener {

	public void contextInitialized(ServletContextEvent event) {
		// 读取所有的资源，放入到ServletContext
		// 使用spring上下文对象
		ServletContext sc = event.getServletContext();
		WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
		ResourcesService resourcesService = (ResourcesService) ctx.getBean("resourcesService");
		List<Resources> resList = resourcesService.findAll();
		StringBuilder sb = new StringBuilder();
		// 将资源转成字符串并用"，"分割
		for (Resources r : resList) {
			sb.append(r.getUrl());
			sb.append(",");
		}
		// 放入到ServletContext sc中
		sc.setAttribute("resList", sb.toString());
	}
	
	public void contextDestroyed(ServletContextEvent arg0) {
	}
}